package com.suse.service.admin.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.common.util.core.StringUtil;
import com.suse.common.util.io.IoUtil;
import com.suse.service.admin.edu.listener.excel.SubjectExcelAnalysisEventListener;
import com.suse.service.admin.edu.mapper.SubjectMapper;
import com.suse.service.admin.edu.model.entity.Subject;
import com.suse.service.admin.edu.model.excel.SubjectExcel;
import com.suse.service.admin.edu.model.vo.TreeSelectSubjectVo;
import com.suse.service.admin.edu.model.vo.TreeTableSubjectVo;
import com.suse.service.admin.edu.service.SubjectService;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-10
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Transactional
    @Override
    public R importSubjectFile(MultipartFile file) {
        InputStream in = null;
        try {
            in = file.getInputStream();
            EasyExcel.read(in, SubjectExcel.class, new SubjectExcelAnalysisEventListener()).sheet().doRead();
        } catch (IOException e) {
            throw new ApiException("导入失败！");
        }finally {
            IoUtil.close(in);
        }
        return R.success("导入成功");
    }

    @Override
    public R exportSbject() {
        return null;
    }

    @Override
    public R getSubjectTreeTable() {
        // 1 先获取所有一级分类
        List<Subject> firstSubjects = lambdaQuery().eq(Subject::getParentId, "0").list();
        // 2 再获取所有二级分类
        List<Subject> secondSubjects = lambdaQuery().ne(Subject::getParentId, "0").list();
        // 3 遍历封装为树形视图对象
        List<TreeTableSubjectVo> treeTableFirstSubjectVos = new ArrayList<>();
        for (Subject firstSubject : firstSubjects) {
            TreeTableSubjectVo treeTableFirstSubjectVo = BeanUtil.copyProperties(firstSubject, TreeTableSubjectVo.class);
            List<TreeTableSubjectVo> treeTableSecondSubjectVos = new ArrayList<>();
            for (Subject secondSubject : secondSubjects) {
                String id = firstSubject.getId();
                String parentId = secondSubject.getParentId();
                if (StringUtil.isEquals(id,parentId)) {
                    TreeTableSubjectVo treeTableSecondSubjectVo = BeanUtil.copyProperties(secondSubject, TreeTableSubjectVo.class);
                    treeTableSecondSubjectVos.add(treeTableSecondSubjectVo);
                }
            }
            treeTableFirstSubjectVo.setChildren(treeTableSecondSubjectVos);
            treeTableFirstSubjectVos.add(treeTableFirstSubjectVo);
        }
        int total = treeTableFirstSubjectVos.size();
        return R.put("total",total).put("records",treeTableFirstSubjectVos).success();
    }

    @Override
    public R getSubjectTreeSelect() {
        // 1 先获取所有一级分类
        List<Subject> firstSubjects = lambdaQuery().select(Subject::getId,Subject::getTitle)
                .eq(Subject::getParentId, "0").list();
        // 2 再获取所有二级分类
        List<Subject> secondSubjects = lambdaQuery().select(Subject::getId,Subject::getTitle,Subject::getParentId)
                .ne(Subject::getParentId, "0").list();
        // 3 遍历封装为树形视图对象
        List<TreeSelectSubjectVo> treeSelectFirstSubjectVos = new ArrayList<>();
        for (Subject firstSubject : firstSubjects) {
            TreeSelectSubjectVo treeSelectFirstSubjectVo = BeanUtil.copyProperties(firstSubject, TreeSelectSubjectVo.class);
            List<TreeSelectSubjectVo> treeSelectSecondSubjectVos = new ArrayList<>();
            for (Subject secondSubject : secondSubjects) {
                String id = firstSubject.getId();
                String parentId = secondSubject.getParentId();
                if (StringUtil.isEquals(id,parentId)) {
                    TreeSelectSubjectVo treeSelectSecondSubjectVo = BeanUtil.copyProperties(secondSubject, TreeSelectSubjectVo.class);
                    treeSelectSecondSubjectVos.add(treeSelectSecondSubjectVo);
                }
            }
            treeSelectFirstSubjectVo.setChildren(treeSelectSecondSubjectVos);
            treeSelectFirstSubjectVos.add(treeSelectFirstSubjectVo);
        }
        return R.success(treeSelectFirstSubjectVos);
    }

}
